package com.jiyun.service.impl;

import cn.hutool.json.JSONUtil;
import cn.hutool.jwt.JWTUtil;
import com.baomidou.mybatisplus.core.metadata.PageList;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.jiyun.feign.*;
import com.jiyun.mapper.MemberMapper;
import com.jiyun.model.day05.User;
import com.jiyun.model.day07.Cataloges;
import com.jiyun.model.day08.Course;
import com.jiyun.model.day10.Record;
import com.jiyun.model.day11.Roll;
import com.jiyun.model.day14.Member;
import com.jiyun.model.day14.Team;
import com.jiyun.service.MemberService;
import com.jiyun.vo.Result;
import com.jiyun.vo.day07.CatalogDto;
import com.jiyun.vo.day08.CourseCto;
import com.jiyun.vo.day08.CourseVo;
import com.jiyun.vo.day10.RecordVo;
import com.jiyun.vo.day10.TopicDto2;
import com.jiyun.vo.day10.TopicVo;
import com.jiyun.vo.day11.RollDto;
import com.jiyun.vo.day11.RollVo;
import com.jiyun.vo.day12.RivalDto;
import com.jiyun.vo.day12.RivalDto2;
import com.jiyun.vo.day12.RivalVo;
import com.jiyun.vo.day14.TeamVo;
import com.jiyun.vo.day15.TraineeDto;
import com.jiyun.vo.day15.TraineeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author yangYingJie
 * @Date 2023/8/22 11：03
 **/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private Feign feign;
    @Autowired
    private FeignCatalog feignCatalog;

    @Override
    public String login(Member member) {
        Member login = feign.login(member);
        if (login != null) {
            Map map = new HashMap();
            map.put("token", login.getId());
            String token = JWTUtil.createToken(map, "1".getBytes());
            redisTemplate.opsForValue().
                    set(token, JSONUtil.toJsonStr(login),
                            120, TimeUnit.MINUTES);
            return token;
        } else {
            return null;
        }
    }

    @Autowired
    private FeignRival feignRival;

    @Override
    public Integer findByIdRival(HttpServletRequest request) {
        String token = request.getHeader("token");
        String o = (String) redisTemplate.opsForValue().get(token);
        User user = JSONUtil.toBean(o, User.class);
        RivalDto rivalDto = new RivalDto();
        String s = user.getId().toString();
        rivalDto.setId(Integer.parseInt(s));
        Result count = feignRival.findCount(rivalDto);
        Integer data = (Integer) count.getData();
        return data;
    }

    @Autowired
    private FeignTrainee feignTrainee;

    @Override
    public Integer findByIdCourse(HttpServletRequest request) {
        String token = request.getHeader("token");
        String o = (String) redisTemplate.opsForValue().get(token);
        User user = JSONUtil.toBean(o, User.class);
        String s = user.getId().toString();
        TraineeDto traineeDto = new TraineeDto();
        traineeDto.setId(Integer.parseInt(s));
        Result count = feignCatalog.findCount(traineeDto);
        Integer data = (Integer) count.getData();
        return data;
    }

    @Override
    public Integer findByIdTrainee(HttpServletRequest request) {
        String token = request.getHeader("token");
        String o = (String) redisTemplate.opsForValue().get(token);
        User user = JSONUtil.toBean(o, User.class);
        String s = user.getId().toString();
        TraineeDto traineeDto = new TraineeDto();
        traineeDto.setId(Integer.parseInt(s));
        Result count2 = feignTrainee.findCount2(traineeDto);
        Integer data = (Integer) count2.getData();
        return data;
    }

    @Autowired
    private FeignCourse feignCourse;

    @Override
    public List<CourseVo> findAll() {
        List<Course> all2 = feignCourse.findAll2();
        List<CourseVo> list1 = new ArrayList<>();
        for (Course course : all2) {
            String ss = (String) redisTemplate.opsForHash().get("点赞", course.getId() + "");
            CourseVo courseVo = new CourseVo();
            BeanUtils.copyProperties(course, courseVo);
            courseVo.setCount(Integer.parseInt(ss));
            list1.add(courseVo);
        }
        List<CourseVo> list = new ArrayList<>();
        CourseVo courseVo1 = null;
        int g = 3;
        for (int j = 0; j < g; j++) {
            int b = -1;
            int a = 0;
            for (int i = 0; i < list1.size(); i++) {
                if (a < list1.get(i).getCount()) {
                    a = list1.get(i).getCount();
                    b = i;
                    courseVo1 = list1.get(i);
                }
            }
            list.add(courseVo1);
            list1.remove(b);
        }
        for (CourseVo courseVo : list) {
            String s = courseVo.getId().toString();
            String num = feignCourse.findNum(Integer.parseInt(s));
            TraineeDto traineeDto = new TraineeDto();
            traineeDto.setIds(num);
            Integer num1 = feignTrainee.findNum(traineeDto);
            courseVo.setNum(num1);
        }
        return list;
    }

    @Override
    public void updateDianZan(Integer id) {
        String ss = (String) redisTemplate.opsForHash().get("点赞", id + "");
        System.out.println(ss);
        int i = Integer.parseInt(ss);
        System.out.println(i);
        i++;
        redisTemplate.opsForHash().put("点赞", id + "", JSONUtil.toJsonStr(i + ""));
    }

    @Override
    public void dinaZanRoll(Integer id) {
        String ss = (String) redisTemplate.opsForHash().get("竞赛点赞", id + "");
        int i = Integer.parseInt(ss);
        i++;
        redisTemplate.opsForHash().put("竞赛点赞", id + "", JSONUtil.toJsonStr(i + ""));
    }

    @Override
    public void qvXiaoRoll(Integer id) {
        String ss = (String) redisTemplate.opsForHash().get("竞赛点赞", id + "");
        int i = Integer.parseInt(ss);
        i--;
        redisTemplate.opsForHash().put("竞赛点赞", id + "", JSONUtil.toJsonStr(i + ""));
    }

    @Override
    public void moRen() {
        List<Course> all2 = feignCourse.findAll2();
        for (Course course : all2) {
            String a = "0";
            redisTemplate.opsForHash().put("点赞", course.getId() + "", JSONUtil.toJsonStr(a));
        }
    }

    @Override
    public List<CourseVo> findReCourse() {
        //查询到了最新的三个课程
        List<Course> desc = feignCourse.findDesc();
        List<CourseVo> vo = new ArrayList<>();
        for (Course course : desc) {
            String ss = (String) redisTemplate.opsForHash().get("点赞", course.getId() + "");
            CourseVo courseVo = new CourseVo();
            BeanUtils.copyProperties(course, courseVo);
            if (ss == null || ss == "") {
                redisTemplate.opsForHash().put("点赞", course.getId() + "", JSONUtil.toJsonStr("0"));
                courseVo.setCount(0);
            } else {
                courseVo.setCount(Integer.parseInt(ss));
            }
            String s = courseVo.getId().toString();
            String num = feignCourse.findNum(Integer.parseInt(s));
            TraineeDto traineeDto = new TraineeDto();
            traineeDto.setIds(num);
            Integer num1 = feignTrainee.findNum(traineeDto);
            if (num1 != null) {
                courseVo.setNum(num1);
            } else {
                courseVo.setNum(0);
            }
            vo.add(courseVo);
        }
        return vo;
    }

    @Autowired
    private FeignRoll feignRoll;

    @Override
    public List<RollVo> findReRoll() {
        List<Roll> desc = feignRoll.findDesc();
        List<RollVo> list = new ArrayList<>();
        for (Roll roll : desc) {
            String dianZan = (String) redisTemplate.opsForHash().get("竞赛点赞", roll.getId() + "");
            RollVo rollVo = new RollVo();
            BeanUtils.copyProperties(roll, rollVo);
            if (dianZan == null || dianZan == "") {
                redisTemplate.opsForHash().put("竞赛点赞", roll.getId() + "", JSONUtil.toJsonStr("0"));
                rollVo.setCount(0);
            } else {
                rollVo.setCount(Integer.parseInt(dianZan));
            }
            Integer trainee = feignRival.findNum(rollVo.getId());
            if (trainee != null) {
                rollVo.setNum(trainee);
            } else {
                rollVo.setNum(0);
            }
            list.add(rollVo);
        }
        return list;
    }

    @Override
    public List<TraineeVo> findReTrainee() {
        List<TraineeVo> reTrainee = feignTrainee.findReTrainee();
        return reTrainee;
    }

    @Override
    public PageInfo<CourseVo> findDetailAll(CourseCto courseCto) {
        PageInfo<CourseVo> cAll = feignCourse.findCAll(courseCto);
        return cAll;
    }


    @Override
    public Map<String, Object> findDetailCourseOne(Integer id, HttpServletRequest request) {
        String token = request.getHeader("token");
        String ids = id + "," + token;
        CourseVo byId = feignCourse.findById(ids);
        CatalogDto catalogDto = new CatalogDto();
        catalogDto.setCid(id);
        catalogDto.setName(token);
        List<Cataloges> all2 = feignCatalog.findAll2(catalogDto);
        Map<String, Object> map = new HashMap<>();
        map.put("vo", byId);
        map.put("shu", all2);
        return map;
    }

    @Override
    public void updateCatalogStatus(CatalogDto dto, HttpServletRequest request) {
        String token = request.getHeader("token");
        dto.setName(token);
        feignCatalog.updateCatalog(dto);
    }

    @Override
    public void addCataloges(String id, HttpServletRequest request) {
        String token = request.getHeader("token");
        id = id + "," + token;
        feignCatalog.addCataloges(id);
    }

    @Override
    public PageInfo<RivalVo> findRivalAll(RivalDto2 dto) {
        PageInfo<RivalVo> cAll = feignRival.findCAll(dto);
        return cAll;
    }

    @Override
    public RivalVo findRivalDetail(String id, HttpServletRequest request) {
        String token = request.getHeader("token");
        id = id + "," + token;
        RivalVo byId = feignRival.findById(id);
        return byId;
    }

    @Override
    public Result findByIdMember(HttpServletRequest request) {
        String token = request.getHeader("token");
        String o = (String) redisTemplate.opsForValue().get(token);
        Member member = JSONUtil.toBean(o, Member.class);
        return Result.success(member);
    }

    @Override
    public Result findAllTeam() {
        List<Team> all3 = feign.findAll3();
        return Result.success(all3);
    }

    @Override
    public void addTeamMember(String id, HttpServletRequest request) {
        String token = request.getHeader("token");
        id = id + "," + token;
        feign.addTeamMember(id);
    }

    @Override
    public void addRivalMember(String id, HttpServletRequest request) {
        String token = request.getHeader("token");
        id = id + "," + token;
        feignRival.addRivalMember(id);
    }

    @Autowired
    private FeignTopic feignTopic;

    @Override
    public Result findCRoll(Record record, HttpServletRequest request) {
        String token = request.getHeader("token");
        String o = (String) redisTemplate.opsForValue().get(token);
        Member member = JSONUtil.toBean(o, Member.class);
        record.setMid(member.getId());
        List<RollVo> cAll = feignRoll.findCAll(record);
        return Result.success(cAll);
    }

    @Override
    public Result findCTopic(TopicDto2 dto) {
        PageInfo<TopicVo> cAll = feignTopic.findCAll(dto);
        return Result.success(cAll);
    }

    @Override
    public Result findCTopic2(TopicDto2 dto) {
        PageInfo<TopicVo> cAll = feignTopic.findCAll2(dto);
        return Result.success(cAll);
    }

    @Override
    public List<Record> findRecord(TopicDto2 dto) {
        List<Record> record = feignRoll.findRecord(dto);
        return record;
    }

    @Override
    public Integer findFen(TopicDto2 dto) {
        Integer endFen = feignRoll.findEndFen(dto);
        return endFen;
    }

    @Override
    public Result addRecord(Record record) {
        feignRoll.addRecord(record);
        return Result.success(null);
}

    @Override
    public Result findCTrainee(TraineeDto dto) {
        PageInfo<TraineeVo> page = feignTrainee.findCAll(dto);
        return Result.success(page);
    }

    @Override
    public TraineeVo findTrianeeById(String id) {
        TraineeVo byId = feignTrainee.findById(id);
        return byId;
    }

    @Override
    public void addTraineeMember(String id) {
        feignTrainee.addTraineeMember(id);
    }

    @Override
    public List<CourseVo> findByIdCataloges(Integer id) {
        List<CourseVo> byIdCataloges = feignCourse.findByIdCataloges(id);
        return byIdCataloges;
    }

    @Override
    public List<RollVo> findTraineeRoll(Integer id) {
        List<RollVo> list = feignRoll.findTraineeRoll(id);
        for (RollVo rollVo : list) {
            Integer rollTopicNum = feignRoll.findRollTopicNum(rollVo.getId());
            rollVo.setCount(rollTopicNum);
            Integer trainee = feignRival.findNum(rollVo.getId());
            if (trainee != null) {
                rollVo.setNum(trainee);
            } else {
                rollVo.setNum(0);
            }

        }
        return list;
    }

    @Override
    public  PageInfo<RollVo> findShowRoll(RollDto dto) {
        PageInfo<RollVo> showAll = feignRoll.findShowAll(dto);
        return showAll;
    }

    @Override
    public List<RivalVo> memberRival(RivalDto2 dto) {
        List<RivalVo> list = feignRival.memberRival(dto);
        return list;
    }

    @Override
    public TeamVo findByIdTeam(Integer id) {
        return feign.findById(id);
    }

    @Override
    public List<RecordVo> findByIdRecord(Integer id) {
        List<RecordVo> byIdRecord = feignRoll.findByIdRecord(id);
        return byIdRecord;
    }

    @Override
    public Integer findRollNum(HttpServletRequest request) {
        String token = request.getHeader("token");
        String o = (String) redisTemplate.opsForValue().get(token);
        Member member = JSONUtil.toBean(o, Member.class);
        Integer rollNum = feignRoll.findRollNum(member.getId());
        return rollNum;
    }


}
